<template>
  <div>
    <dc-header @submit="saveEdite"
               :page-to="'/simAccountTool'"
               :title="param.accountFlag === 2 ? '编辑支出' : '编辑收入'"
               :pageToQuery = "{beginTime: this.param.beginTime}"
               action-title="保存"></dc-header>
    <div class="head-top">
      <div class="common-card">
        <mt-cell title="" v-if="param.accountFlag === 1" class="main-title account-border-b">
          <span slot="icon">收入项</span>
          <span class="right-link" @click="copyLastMonth()" v-if="isCopy">
                <span class="theme-color right-title"><i class="iconfont icon-account-copy"></i>复制上月数据
                </span>
              </span>
        </mt-cell>
        <mt-cell title="" v-if="param.accountFlag === 2" class="main-title account-border-b">
          <span slot="icon">支出项</span>
          <span class="right-link" @click="copyLastMonth()" v-if="isCopy">
                <span class="theme-color right-title"><i class="iconfont icon-account-copy"></i>复制上月数据
                </span>
              </span>
        </mt-cell>
        <dc-row type="flex" justify="center" class="bg-white account-input-list account-border-b"
                v-for="data in accountToolList" :key="data.accountCode">
          <dc-col span="10">
            <svg class="account-color-icon" aria-hidden="true">
              <use :xlink:href="'#'+data.accountIcon"></use>
            </svg>
            <span class="input-label">{{data.accountName}}</span>
            <dc-tooltip v-if="data.accountName === '原料成本' && param.accountFlag ===2" :place="'bottom-left'"
                        :msg="'原料成本为系统自动更新，不可修改'"></dc-tooltip>
            <dc-tooltip v-if="data.accountName === '营收收入' && param.accountFlag ===1" :place="'bottom-left'"
                        :msg="'营业收入为系统自动更新，不可修改'"></dc-tooltip>

          </dc-col>
          <dc-col span="14">
            <div class="input-div">
              <input class="input-data" :maxLength = "1" :name="data.accountName" v-validate="{ rules: { required: true} }"
                     :class="{'is-danger': errors.has(data.accountName)}"
                     type="number" value="600" v-model="data.accountNum"
                     :disabled="data.isOptional === 0 ? true : false"/>
              <i v-show="data.isOptional != 0" class="iconfont icon-edite"></i>
            </div>
          </dc-col>
        </dc-row>
      </div>
    </div>
  </div>
</template>
<script>
  import header from '@/common/plugins/header';
  import mintUi from '@/common/mintUi';
  import tooltip from '@/common/plugins/tooltip';
  import {col, row} from '@/common/Flex';
  import api from '@/common/api/account';
  /**
   * @namespace  components/account/accountTools/simAccountEdit
   * @author lxr
   */
  export default {
    data () {
      return {
        param: this.$route.query.param,
        accountToolList: this.$route.query.data,
        isCopy: false
      };
    },
    created () {
      var paramMonth = new Date(this.param.beginTime).getMonth();
      var currentMonth = new Date().getMonth();
      if (paramMonth === currentMonth) {
        this.isCopy = true;
      }
    },
    components: {
      'dc-header': header,
      'dc-col': col,
      'dc-row': row,
      'dc-tooltip': tooltip
    },
    methods: {
      saveEdite () {
        this.$validator.validateAll().then((result) => {
          if (result) {
            this.param['simpleDTOS'] = this.accountToolList;
            api.saveAccountData(this.param).then(res => {
              mintUi.openToast('修改成功');
              this.$router.push({path: '/simAccountTool', query: this.param});
            });
          } else {
            mintUi.openToast(this.errors.items[0].msg);
          };
        });
      },
      copyLastMonth () {
        api.accountCopyLastMonth({accountFlag: this.param.accountFlag}).then(res => {
          let a = this.accountToolList[0].accountNum;
          console.log(this.accountToolList);
          this.accountToolList = res;
          this.accountToolList[0].accountNum = a;
          mintUi.openToast('复制成功');
        });
      }
    }
  };
</script>
<style lang="scss" scoped>
  @import "../../../assets/css/variable";
  @import "../account";
  .input-div {
    position: relative;
    .icon-edite {
      position: absolute;
      right: 0;
      width: 20px;
      height: 20px;
      top: 0;
      color: #bbb;
      font-size: 12px;
    }
  }
</style>

